package com.xiva.es.demo;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.util.IOUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

public class ImageBase64
{
    public static void main(String[] args) throws IOException
    {
//        String strImg = readFileStr();
//
//        GenerateImage(strImg);

        tecentDatToImage();
    }


    public static String readFileStr() throws IOException
    {
        String imgFile = "F://opt/0d3220dd8f448c8ef7526053a855f5ac.dat";

        String fileStr = FileUtils.readFileToString(new File(imgFile), "UTF-8");

        return fileStr;
    }

    // 图片转化成base64字符串
    public static String GetImageStr()
    {
        // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        String imgFile = "F://opt/0d3220dd8f448c8ef7526053a855f5ac.dat";

        // 待处理的图片
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    }

    /**
     * JPEG (jpg)，文件头：FFD8FF
     * PNG (png)，文件头：89504E47
     * GIF (gif)，文件头：47494638
     */
    public static void tecentDatToImage()
    {
        int jpegType = 0xFFD8;
        int pngType = 0x8950;
        int gifType = 0x4749;

        FileInputStream in = null;
        FileOutputStream out = null;
        try
        {
            String sourceFileUrl = "F://opt/2c9d2bdeef39fc6655e757be2c9799aa.dat";//文件存放目录

            String targetFileUrl = "D://3.png";//输出目录
            in = new FileInputStream(sourceFileUrl);
            BufferedInputStream bis = new BufferedInputStream(in);

            out = new FileOutputStream(targetFileUrl);
            BufferedOutputStream bos = new BufferedOutputStream(out);

            int data = 0;
            while ((data = bis.read()) != -1)
            {

                int res = data^jpegType;



                //将读取到的字节异或上一个数，加密输出
                bos.write(data ^ (0xCB));//0xc6可以改
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            //在finally中关闭开启的流
            if (in != null)
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (out != null)
            {
                try
                {
                    out.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    // base64字符串转化成图片
    public static boolean GenerateImage(String imgStr)
    { // 对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try
        {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i)
            {
                if (b[i] < 0)
                {// 调整异常数据
                    b[i] += 256;
                }
            }
            // 生成jpeg图片
            String imgFilePath = "D://222.jpg";// 新生成的图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }
}
